/*
键盘操作控制
*/
window._MK = {

    _SENCE:null,

    _Dir:null,

    showKeyInfo:function(){

        return "WASD:移动 Q:仓库 E:人脉 \nZ:地图 R:存档";

    },

    initlisten:function(sence){

        _MK._SENCE = sence;

        _MK._Dir = null;

        cc.systemEvent.on('keydown', this.onKeyDown, sence);
		
		cc.systemEvent.on('keyup', this.onKeyUp, sence);

    },

    onKeyUp:function(event){
        _MK._Dir = null;
	},

    /*
    onButtonDownDir:function(dir){

        if(_MAP.isMoving)return;
		
		if(_MK._Dir==dir)return;
				
		_MK._Dir = dir;

        var startPoint = _MANAGERMAP.getMapPos(_MAP.playerNode.x,_MAP.playerNode.y,_MAP.maxH,_MAP.maxW);
        //console.log(startPoint);

        if(
            !startPoint
            ||!startPoint.x
            ||!startPoint.y
        ){
            return ;
        }

        var sx = startPoint.x;
        var sy = startPoint.y;
        var tx = 0;
        var ty = 0;
        var path = [];
        var ispass = true;
        if(_MK._Dir=='上'){
            ty=-1;
        }else if(_MK._Dir=='下'){
            ty=1;
        }else if(_MK._Dir=='左'){
            tx=-1;
        }else if(_MK._Dir=='右'){
            tx=1;
        }
        while(ispass){

            sx+=tx;
            sy+=ty;

            if(_MANAGERMAP.isMapPointCanTouchByMap(_MAP.tiledMap,sx,sy,_MAP.maxH,_MAP.maxW)){
                path.push([sx,sy]);
            }else{
                ispass=false;
            }

        } 

        //console.log('可通过路径:',path);

        if(path.length>0){
            _MAP.movePath = path;
            _MAP.isMoving = true;
            _MAP.moveIndex = 0;
            _MAP.mapmove();
        }
	},
    */


    getNextPos:function(){
        var startPoint = _MANAGERMAP.getMapPos(_MAP.playerNode.x,_MAP.playerNode.y,_MAP.maxH,_MAP.maxW);

        if(
            !startPoint
            ||!startPoint.x
            ||!startPoint.y
        ){
            return null;
        }

        if(_MK._Dir=='上'){
            startPoint.y--;
        }else if(_MK._Dir=='下'){
            startPoint.y++;
        }else if(_MK._Dir=='左'){
            startPoint.x--;
        }else if(_MK._Dir=='右'){
            startPoint.x++;
        }

        return startPoint;
    },

    moveByDir:function(){

        var nextpos = _MK.getNextPos();

        if(
            nextpos
            &&_MANAGERMAP.isMapPointCanTouchByMap(_MAP.tiledMap,nextpos.x,nextpos.y,_MAP.maxH,_MAP.maxW)
        ){
            _MAP.moveToPoint(_MANAGERMAP.getMapNodePosByTiled(nextpos.x,nextpos.y,_MAP.maxH,_MAP.maxW),false);
        }

    },

    onButtonDownDir:function(dir){
				
		_MK._Dir = dir;

        if(!_MAP.isMoving){

            _MK.moveByDir();
        }

        
	},

    onKeyDown(e){

        //27 esc
        //81 q
        //69 e
        //49-52 1-4
        //z 90
        //x 88
        //c 67

        //r 82
        //t 84
        //f 70
        //v 86


        if(e.keyCode=='27'){//esc 退出
            return;
        }

        if(!_MAP.canMove())return;

        //console.log('键盘',e.keyCode);

        if(e.keyCode=='81'){//q 仓库

            _MG.initUi(_MAP.node,{sence:_STYPE._GOODSSENCE});

        }else if(e.keyCode=='69'){//e 人脉

            _MPEOPLE.showPeople(_MAP.node,null);

        }else if(e.keyCode=='90'){//z 地图

            _CHAPTER.showUi(_MAP.node);

        }else if(e.keyCode=='82'){//r 存档

            _MAP.node.getChildByName('savelabel').active = true;

        }else if(e.keyCode=='87'){//上
			
			_MK.onButtonDownDir('上');
			
		}else if(e.keyCode=='83'){
			
			_MK.onButtonDownDir('下');
			
		}else if(e.keyCode=='65'){
			
			_MK.onButtonDownDir('左');
			
		}else if(e.keyCode=='68'){
			
			_MK.onButtonDownDir('右');
			
		}
	},

}